package com.ygou.controller;


import java.sql.Date;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.ygou.pojo.Admininfo;
import com.ygou.pojo.GoodsType;
import com.ygou.service.impl.GoodTypeServiceImpl;
import com.ygou.utils.AppendContentToFile;

@Controller
@RequestMapping(value="/goodtype")
@SessionAttributes(value={"typeD","PCount","typeList"})
public class GoodsTypeController{
	@Autowired
	GoodTypeServiceImpl gtService;
	@Autowired
	HttpServletResponse response;
	@RequestMapping(value="/showAllTypes",produces="application/json;charset=UTF-8")
	public @ResponseBody List<GoodsType> showAll(ModelMap model){
		List<GoodsType> typeList=gtService.showAll();
		return typeList;
	}
	
	@RequestMapping("/showDetail")
	public String showDetail(Integer id,ModelMap model){
		GoodsType typeD=gtService.selById(id.longValue());
		model.put("typeD", typeD);
		int PCount=gtService.selPCount(typeD.getParentid().intValue());
		model.put("PCount", PCount);
		this.showAll(model);
		return "redirect:/admin/product-category-add.jsp";
	}
	//执行禁用/删除类型
	@RequestMapping("/doDelete")
	public String delete(HttpSession session,Integer id,ModelMap model){
		Long Lid=new Long(id);
		gtService.doDelete(Lid);
		Admininfo admin= (Admininfo)session.getAttribute("admin");
		int adminid=100;
		if(admin!=null){
			adminid=admin.getAdminid();
		}
		AppendContentToFile.fileLogPrint(adminid+"", "启用/禁用了商品类型(ID:"+id+")");
		return "redirect:/goodtype/showDetail?id="+id;
	}
	@RequestMapping("/ShowBeDeleted")
	public String showDelete(ModelMap model){
		model.put("DeletedTypes",gtService.showDeleted());
		return "redirect:/admin/";
	}
	//找回删除的类型
	@RequestMapping(value="/TypeGetBack",produces="application/json;charset=utf-8")
	public String ReBack(Integer id,ModelMap model){
		Long Lid=new Long(id);
		gtService.ReBack(Lid);
		return "redirect:/goodtype/showDetail?id="+id;
	}
	@RequestMapping(value="/doUpdate",produces="application/json;charset=UTF-8")
	public String doUpdate(HttpSession session,int TypeId,String TypeName,int sortOrder,java.util.Date createtime,java.util.Date lasttime,Boolean isParent,int status,int parentid,ModelMap model){
		GoodsType g=new GoodsType();
		Long a=new Long(TypeId);
		g.setId(a);
		g.setName(TypeName);
		g.setSortOrder(sortOrder);
		g.setIsParent(isParent);
		g.setStatus(status);
		Long pid=new Long(parentid);
		g.setParentid(pid);
		g.setCreaettime(createtime);
		g.setLasttime(new Date(Calendar.getInstance().getTimeInMillis()));
		gtService.doUpdate(g);
		List<GoodsType> typeList=gtService.showAll();
		model.put("typeList", typeList);
		Admininfo admin= (Admininfo)session.getAttribute("admin");
		int id=100;
		if(admin!=null){
			id=admin.getAdminid();
		}
		AppendContentToFile.fileLogPrint(id+"", "对"+TypeId+"号商品类型进行修改");
		return "redirect:/admin/product-category-add.jsp";
	}
	@RequestMapping(value="/checkname",produces="application/json;charset=UTF-8")
	public int checkname(String name){
		return gtService.checkName(name);
	}
	//打开添加子页面
	@RequestMapping("/openAddSon")
	public String openAdd(int pid,ModelMap model){
		GoodsType pT=gtService.selById(new Long(pid));
		model.put("pT", pT);
		int PCount=gtService.selPCount(pT.getId().intValue());
		model.put("PCount", PCount);
		return "redirect:/admin/add_son_category.jsp";
	}
	@RequestMapping(value="/doAdd",produces="html/text;charset=UTF-8")
	public @ResponseBody String doAdd(HttpSession  session,String TypeName,Long pid,int sortOrder,ModelMap model){
		
		GoodsType g=new GoodsType();
		g.setName(TypeName);
		g.setParentid(pid);
		if(pid.intValue()==0||pid==null){
			g.setIsParent(true);
		}
		g.setCreaettime(new Date(Calendar.getInstance().getTimeInMillis()));
		g.setLasttime(g.getCreaettime());
		g.setStatus(1);
		g.setSortOrder(sortOrder);
		gtService.insert(g);
		GoodsType newG=gtService.selByName(TypeName);
		List<GoodsType> typeList=gtService.showAll();
		model.put("typeList", typeList);
		
		Admininfo admin= (Admininfo)session.getAttribute("admin");
		int id=100;
		if(admin!=null){
			id=admin.getAdminid();
		}
		AppendContentToFile.fileLogPrint(id+"", "添加了名字叫：-"+TypeName+"-的商品类型");
		return "/goodtype/showDetail?id="+newG.getId(); 
	}
	@RequestMapping("/addPaCate")
	public String openAddPa(ModelMap model){
		int PCount=gtService.selPCount(0);
		model.put("PCount", PCount);
		
		return "redirect:/admin/add_pa_category.jsp";
	}
	@RequestMapping(value="/checkTypeName",produces="application/json;charset=UTF-8")
	public @ResponseBody int checkName(String TypeName){
		return gtService.checkName(TypeName);
	}
	
	/*************************/
	//根据查询
	@RequestMapping(value="/types",produces="application/json;charset=UTF-8")
	public @ResponseBody Map<String, Object> types(ModelMap model){
		Map<String, Object> map=gtService.selectByExample();
		return map;
	}
	@RequestMapping(value="/ptypeid")
	public String ptypeid(int parentid,ModelMap model){
		List<GoodsType> list=gtService.selectByparentid(parentid);
		model.put("list", list);
		return "/list";
	}
	
}